<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12" :offset="6">
        <el-card>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <el-col :span="24">
                <el-form-item label="公司名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入公司名称" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="激活码" prop="code">
                  <el-input v-model="form.code" placeholder="请输入激活码" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {  getSysRegister,updateSysRegister } from "@/api/system/sysRegister";

export default {
  name: "SysRegister",
  data() {
    var isValidString = (rule, str, callback) => {
      var regex = /^BA\d{10}$/;
      if (regex.test(str)) {
        var sum = 0;
        for (var i = 0; i < str.length; i++) {
          if (i >= 2) {
            sum += Number(str[i]);
          }
        }
        if (sum % 11 === 0){
          callback();
        }
      }
      callback(new Error('激活码不符合规则'));
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统注册表格数据
      sysRegisterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "激活码不能为空", trigger: "blur" },
          { validator: isValidString, trigger: 'blur' }
        ],
        name: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.handleUpdate();
  },
  methods: {
    /** 修改按钮操作 */
    handleUpdate() {
      getSysRegister(null).then(response => {
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateSysRegister(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            history.go(0);
          });
        }
      });
    },
  }
};
</script>
